var VisitSettingForm = function () {
  var template = /* html */ `
    <div class="visit-setting-form" v-loading.fullscreen.lock="loading">
      <template v-if="isShow">
        <el-form label-width="auto">
          <el-form-item label="回访评级">
            <el-select class="form-input" filterable v-model="selectVisit" value-key="visitType">
              <el-option v-for="repairVisit in repairVisitList" :value="repairVisit" :label="repairVisit.visitType">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="回访占比">
            <span v-if="selectVisit!==''">{{selectVisit.percent}}%</span>
          </el-form-item>
        </el-form>
        <div style="text-align:center;margin-top:20px">
          <el-button type="primary" @click="toSetting">设置</el-button>
        </div>
      </template>
    </div>
  `;

  return {
    props: {
      repair: {
        type: Object,
        required: true
      },
      dropdownOption: {
        type: Object,
        default: function () {
          return {};
        }
      }
    },

    data: function () {
      return {
        loading: false,
        isShow: false,
        repairVisitList: [],
        selectVisit: ''
      };
    },

    created: function () {
      var self = this;
      this.setRepairVisitList().then(function (repairVisitList) {
        var visitType = self.repair.visitType;

        if (SysUtil.isEmpty(visitType)) {
          self.selectVisit = '';
        } else {
          var repairVisit = _.find(repairVisitList, {
            visitType: visitType
          });
          if (repairVisit !== undefined) {
            self.selectVisit = repairVisit;
          } else {
            self.selectVisit = '';
          }
        }
        self.isShow = true;
      });
    },

    methods: {
      setRepairVisitList: function () {
        var dropdownOption = this.dropdownOption;
        var repairVisitList = dropdownOption.repairVisitList;
        if (repairVisitList !== undefined) {
          this.repairVisitList = repairVisitList;
          return Promise.resolve(repairVisitList);
        }

        this.loading = true;
        var self = this;
        return ajaxRequest('workOrder/getBasicInfoForSalaryList', {
          type: 'vt',
          status: 1
        }).then(function (res) {
          var repairVisitList = res.returnData;
          for (var i = 0; i < repairVisitList.length; i++) {
            var repairVisit = repairVisitList[i];
            var percent = repairVisit.visitPercent;
            percent = _.round(percent * 100, 0);
            repairVisit.percent = percent;
          }

          dropdownOption.repairVisitList = repairVisitList;
          self.repairVisitList = repairVisitList;
          self.loading = false;
          return repairVisitList;
        }).catch(function () {
          self.loading = false;
          return [];
        });
      },

      toSetting: function () {
        var selectVisit = this.selectVisit;
        if (selectVisit === '') {
          this.$alert('请选择回访评级', '信息');
          return;
        }

        var visitType = selectVisit.visitType;
        var repair = this.repair;
        var postData = {
          id: repair.id,
          visitType: visitType
        };

        var salaryType = repair.salaryType;
        if (salaryType !== null) {
          postData.salaryType = salaryType;
          postData.salary = repair.salary;
          postData.starType = repair.starType;
        }

        this.loading = true;
        var self = this;
        ajaxRequest('workOrder/updateWorkOrderApplyForVisit', postData).then(function () {
          self.loading = false;

          self.$alert('该工单的回访评价设置成功', '信息', {
            callback: function () {
              self.$emit('setting-success', selectVisit);
            }
          });
        }).catch(function () {
          self.loading = false;
        });
      }
    },

    template: template
  };
}();

var SalarySettingForm = function () {
  var template = /* html */ `
    <div class="salary-setting-form" v-loading.fullscreen.lock="loading">
      <template v-if="isShow">
        <el-form label-width="auto">
          <el-form-item label="维修评级">
            <el-select class="form-input" filterable v-model="selectSalary" value-key="salaryType">
              <el-option v-for="repairSalary in repairSalaryList" :value="repairSalary" :label="repairSalary.salaryType">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="维修薪资">
            <span v-if="selectSalary!==''">{{selectSalary.salary}}</span>
          </el-form-item>
        </el-form>

        <div style="text-align:center;margin-top:20px">
          <el-button type="primary" @click="toSetting">设置</el-button>
        </div>
      </template>
    </div>
  `;

  return {
    props: {
      repair: {
        type: Object,
        required: true
      },
      dropdownOption: {
        type: Object,
        default: function () {
          return {};
        }
      }
    },

    data: function () {
      return {
        loading: false,
        isShow: false,
        repairSalaryList: [],
        selectSalary: ''
      };
    },

    created: function () {
      var self = this;
      this.setRepairSalaryList().then(function (repairSalaryList) {
        var salaryType = self.repair.salaryType;
        if (SysUtil.isEmpty(salaryType)) {
          self.selectSalary = '';
        } else {
          var repairSalary = _.find(repairSalaryList, {
            salaryType: salaryType
          });
          if (repairSalary !== undefined) {
            self.selectSalary = repairSalary;
          } else {
            self.selectSalary = '';
          }
        }
        self.isShow = true;
      });
    },

    methods: {
      setRepairSalaryList: function () {
        var dropdownOption = this.dropdownOption;
        var repairSalaryList = dropdownOption.repairSalaryList;
        if (repairSalaryList !== undefined) {
          this.repairSalaryList = repairSalaryList;
          return Promise.resolve(repairSalaryList);
        }

        this.loading = true;
        var self = this;
        return ajaxRequest('workOrder/getBasicInfoForSalaryList', {
          type: 'st',
          status: 1
        }).then(function (res) {
          var repairSalaryList = res.returnData;
          dropdownOption.repairSalaryList = repairSalaryList;
          self.repairSalaryList = repairSalaryList;
          self.loading = false;
          return repairSalaryList;
        }).catch(function () {
          self.loading = false;
          return [];
        });
      },

      toSetting: function () {
        var selectSalary = this.selectSalary;
        if (selectSalary === '') {
          this.$alert('请选择维修评级', '信息');
          return;
        }

        var repair = this.repair;
        this.loading = true;
        var self = this;
        ajaxRequest('workOrder/updateWorkOrderApplyForSalary', {
          id: repair.id,
          salaryType: selectSalary.salaryType,
          salary: selectSalary.salary,
          starType: repair.starType,
          visitType: repair.visitType
        }).then(function () {
          self.loading = false;

          self.$alert('该工单的维修评价设置成功', '信息', {
            callback: function () {
              self.$emit('setting-success', selectSalary);
            }
          });
        }).catch(function () {
          self.loading = false;
        });
      }
    },

    template: template
  };
}();
